package com.dd.shop4j.console.goods.controller;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.dd.shop4j.common.utils.ImageUtils;
import com.dd.shop4j.console.goods.service.GoodsBrandManagerService;
import com.dd.shop4j.goods.model.GoodsBrand;

/**
 * 商品品牌
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/brand")
public class GoodsBrandController {
	
	@Resource GoodsBrandManagerService goodsBrandManagerService;
	
	@RequestMapping
	public ModelAndView init() {
		ModelAndView view = new ModelAndView("/admin/goods/brand_list");
		view.addObject("brand_list", goodsBrandManagerService.getAll());
		return view;
	}
	
	@RequestMapping("/insert")
	public ModelAndView insert() {
		ModelAndView view = new ModelAndView("/admin/goods/brand_form");
		view.addObject("brand", new GoodsBrand());
		return view;
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insert(GoodsBrand brand, MultipartFile logoUrlFile) {
		String uploadDir = ImageUtils.simpleUpload(logoUrlFile);
		if (StringUtils.isNotEmpty(uploadDir)) {
			brand.setLogoUrl(uploadDir);
		}
		goodsBrandManagerService.insertBrand(brand);
		return "redirect:/admin/brand";
	}
	
	@RequestMapping(value = "/update/{brandId}", method = RequestMethod.GET)
	public ModelAndView update(@PathVariable("brandId") Integer brandId) {
		ModelAndView view = new ModelAndView("/admin/goods/brand_form");
		view.addObject("brand", goodsBrandManagerService.loadBrand(brandId));
		return view;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(GoodsBrand brand, MultipartFile logoUrlFile) {
		String uploadDir = ImageUtils.simpleUpload(logoUrlFile);
		if (StringUtils.isNotEmpty(uploadDir)) {
			brand.setLogoUrl(uploadDir);
		}
		goodsBrandManagerService.updateBrand(brand);
		return "redirect:/admin/brand";
	}
	
	@RequestMapping("/delete/{brandId}")
	public String delete(@PathVariable("brandId") Integer brandId) {
		goodsBrandManagerService.deleteBrand(brandId);
		return "redirect:/admin/brand";
	}
	
}
